﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.SinhVienNamHocService;
namespace eTMS.Controllers{
public class SinhVienNamHocControllerBase : CommonController{
	public SinhVienNamHocControllerBase()
		: base()
	{	
		this.dao = new SinhVienNamHocServiceClient();
	}
	~SinhVienNamHocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private SinhVienNamHocEntity oSinhVienNamHoc;	
	public SinhVienNamHocEntity SinhVienNamHocObject{
		get{
			return oSinhVienNamHoc;
		}
		set{
			oSinhVienNamHoc=value;
		}
	}
	protected SinhVienNamHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSSinhVienNamHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSSinhVienNamHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oSinhVienNamHoc==null)return false;
		bool result=true;
		try{
			oSinhVienNamHoc = dao.SaveRet(oSinhVienNamHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListSinhVienNamHoc.Add(oSinhVienNamHoc);
		}
		return result;
	}
	public bool Update(){
		if(oSinhVienNamHoc==null)return false;
		return dao.Update(oSinhVienNamHoc);
	}
	public bool Delete(){
		if(oSinhVienNamHoc==null)return false;
		bool retvalue = dao.Delete(oSinhVienNamHoc);
		if(retvalue){			
			_ListSinhVienNamHoc.Remove(oSinhVienNamHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 SinhVienNamHoc_ID){
		bool retvalue = dao.DeleteByID(SinhVienNamHoc_ID);		
		if(retvalue){
			SinhVienNamHocEntity oDeleted=FindSinhVienNamHoc(SinhVienNamHoc_ID);
			if(oDeleted!=null){
				_ListSinhVienNamHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oSinhVienNamHoc = new SinhVienNamHocEntity();
	}
    //Copy data from data row in grid to object
    public SinhVienNamHocEntity MapDataRowToObject(DataRow row){
		SinhVienNamHocEntity o = new SinhVienNamHocEntity();
		 if(!row.IsNull("DiemTongKet"))
			o.DiemTongKet=ConvertNumber(row["DiemTongKet"]);
		 if(!row.IsNull("DiemRenLuyen"))
			o.DiemRenLuyen=ConvertNumber(row["DiemRenLuyen"]);
		 if(!row.IsNull("XepLoaiTongKet_ID"))
			o.XepLoaiTongKet_ID=ConvertInt(row["XepLoaiTongKet_ID"]);
		 if(!row.IsNull("XepLoaiRenLuyen_ID"))
			o.XepLoaiRenLuyen_ID=ConvertInt(row["XepLoaiRenLuyen_ID"]);
		 if(!row.IsNull("NamThu"))
			o.NamThu=ConvertByte(row["NamThu"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("SinhVienNamHoc_ID"))
		    o.SinhVienNamHoc_ID=(Int32)(row["SinhVienNamHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(SinhVienNamHocEntity o,DataRow row){
		row["DiemTongKet"]=o.DiemTongKet;
		row["DiemRenLuyen"]=o.DiemRenLuyen;
		row["XepLoaiTongKet_ID"]=o.XepLoaiTongKet_ID;
		row["XepLoaiRenLuyen_ID"]=o.XepLoaiRenLuyen_ID;
		row["NamThu"]=o.NamThu;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["SinhVienNamHoc_ID"]=o.SinhVienNamHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SinhVienNamHocObject,row);
    }
	public void LoadObject(Int32 SinhVienNamHoc_ID){
		SinhVienNamHocEntity oLoad=FindSinhVienNamHoc(SinhVienNamHoc_ID);
		if(oLoad==null){
			oSinhVienNamHoc=this.LoadSinhVienNamHoc(SinhVienNamHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oSinhVienNamHoc.IsNullSinhVienNamHoc_ID;
			if(!bNotIsNull){
				this.ListSinhVienNamHoc.Add(oSinhVienNamHoc);
			}
		}
		else{
			oSinhVienNamHoc=oLoad;
		}
	}
	#region CachedList SinhVienNamHoc
	private static List<SinhVienNamHocEntity> _ListSinhVienNamHoc=new List<SinhVienNamHocEntity>();
    public List<SinhVienNamHocEntity>ListSinhVienNamHoc{
		get{
			return _ListSinhVienNamHoc;
		}
	}
	public static List<SinhVienNamHocEntity>ListSinhVienNamHocStatic{
		get{
			return _ListSinhVienNamHoc;
		}
	}
	public SinhVienNamHocEntity FindSinhVienNamHoc(Int32 SinhVienNamHoc_ID){
		SinhVienNamHocEntity oResult=this.ListSinhVienNamHoc.Find(delegate(SinhVienNamHocEntity oFind){
			bool result=true; 
			result&=oFind.SinhVienNamHoc_ID.Equals(SinhVienNamHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllSinhVienNamHoc(bool clearCache){
		if(_ListSinhVienNamHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		SinhVienNamHocServiceClient dao = new SinhVienNamHocServiceClient();
		_ListSinhVienNamHoc=new List<SinhVienNamHocEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public SinhVienNamHocEntity LoadSinhVienNamHoc(Int32 SinhVienNamHoc_ID){			
		SinhVienNamHocEntity o=dao.SelectOne(SinhVienNamHoc_ID);		
		return o;
	}
	#endregion
}
}
